#!/bin/sh

set -x
context=''
# poor mans option parsing
if [ -n "$1" ]; then
	if [ "$1" != "odbc" ]; then
		echo "invalid argument"
		exit 1
	fi
	context=odbc
	if [ -n "$2" ]; then
		echo "too many arguments"
		exit 1
	fi
fi

export PDNS=/usr/sbin/pdns_server
export PDNS2=$PDNS
export SDIG=/usr/bin/sdig
export NSEC3DIG=/usr/bin/nsec3dig
export NOTIFY=/usr/bin/pdns_notify
export SAXFR=/usr/bin/saxfr
export ZONE2SQL=/usr/bin/zone2sql
export ZONE2JSON=/usr/bin/zone2json
export PDNSUTIL=/usr/bin/pdnsutil
export PDNSCONTROL=/usr/bin/pdns_control

export GEM_HOME=${PWD}/gems
mkdir -p $GEM_HOME
export PATH="${GEM_HOME}/bin:$PATH"

if [ -z "$context" ]; then
	cd modules/remotebackend
	ruby -S bundle install
	cd ../../
fi

MODULES=""

for dir in /usr/lib/x86_64-linux-gnu/pdns /usr/lib64/pdns; do
  if [ -d $dir ]; then
    MODULES=$dir
    break
  fi
done
[ -z $MODULES ] && echo "No module directory found" >&2 && exit 1

# Symlink the modules on the system
cd regression-tests/modules
for backend in *.so; do
  ln -sf $MODULES/$backend $backend
done

cd ..

EXITCODE=0

if [ -z "$context" ]; then
	export geoipregion=oc geoipregionip=1.2.3.4
	./timestamp ./start-test-stop 5300 bind-both || EXITCODE=1
	./timestamp ./start-test-stop 5300 bind-dnssec-both || EXITCODE=1

	# No PKCS#11 in packages
	#SETUP_SOFTHSM=y ./timestamp ./start-test-stop 5300 bind-dnssec-pkcs11 || EXITCODE=1
	./timestamp ./start-test-stop 5300 bind-dnssec-nsec3-both || EXITCODE=1
	./timestamp ./start-test-stop 5300 bind-dnssec-nsec3-optout-both || EXITCODE=1
	./timestamp ./start-test-stop 5300 bind-dnssec-nsec3-narrow || EXITCODE=1
	./timestamp ./start-test-stop 5300 bind-hybrid-nsec3 || EXITCODE=1

	# Adding extra IPs to docker containers in not supported :(
	#./timestamp ./start-test-stop 5300 geoipbackend || EXITCODE=1
	#./timestamp ./start-test-stop 5300 geoipbackend-nsec3-narrow || EXITCODE=1

	./timestamp ./start-test-stop 5300 gmysql-nodnssec-both || EXITCODE=1
	./timestamp ./start-test-stop 5300 gmysql-both || EXITCODE=1
	./timestamp ./start-test-stop 5300 gmysql-nsec3-both || EXITCODE=1
	./timestamp ./start-test-stop 5300 gmysql-nsec3-optout-both || EXITCODE=1
	./timestamp ./start-test-stop 5300 gmysql-nsec3-narrow || EXITCODE=1

	./timestamp ./start-test-stop 5300 gpgsql-nodnssec-both || EXITCODE=1
	./timestamp ./start-test-stop 5300 gpgsql-both || EXITCODE=1
	./timestamp ./start-test-stop 5300 gpgsql-nsec3-both || EXITCODE=1
	./timestamp ./start-test-stop 5300 gpgsql-nsec3-optout-both || EXITCODE=1
	./timestamp ./start-test-stop 5300 gpgsql-nsec3-narrow || EXITCODE=1

	./timestamp ./start-test-stop 5300 gsqlite3-nodnssec-both || EXITCODE=1
	./timestamp ./start-test-stop 5300 gsqlite3-both || EXITCODE=1
	./timestamp ./start-test-stop 5300 gsqlite3-nsec3-both || EXITCODE=1
	./timestamp ./start-test-stop 5300 gsqlite3-nsec3-optout-both || EXITCODE=1
	./timestamp ./start-test-stop 5300 gsqlite3-nsec3-narrow || EXITCODE=1

	./timestamp ./start-test-stop 5300 mydns || EXITCODE=1

	./timestamp ./start-test-stop 5300 opendbx-sqlite3 || EXITCODE=1

	./timestamp timeout 120s ./start-test-stop 5300 remotebackend-pipe || EXITCODE=1
	./timestamp timeout 120s ./start-test-stop 5300 remotebackend-pipe-dnssec || EXITCODE=1
	./timestamp timeout 120s ./start-test-stop 5300 remotebackend-unix || EXITCODE=1
	./timestamp timeout 120s ./start-test-stop 5300 remotebackend-unix-dnssec || EXITCODE=1
	./timestamp timeout 120s ./start-test-stop 5300 remotebackend-http || EXITCODE=1
	./timestamp timeout 120s ./start-test-stop 5300 remotebackend-http-dnssec || EXITCODE=1

	./timestamp timeout 120s ./start-test-stop 5300 lua2
	./timestamp timeout 120s ./start-test-stop 5300 lua2-dnssec

	# No 0MQ in the PowerDNS packages
	#./timestamp timeout 120s ./start-test-stop 5300 remotebackend-zeromq || EXITCODE=1
	#./timestamp timeout 120s ./start-test-stop 5300 remotebackend-zeromq-dnssec || EXITCODE=1

	./timestamp ./start-test-stop 5300 tinydns || EXITCODE=1

	cd ../modules/luabackend/test2/
	../../../regression-tests/timestamp ./runtest || EXITCODE=1

	cd ../../../regression-tests.nobackend/

	./runtests || EXITCODE=1
elif [ "$context" = "odbc" ]; then
	cat > ~/.odbc.ini << __EOF__
[pdns-sqlite3-1]
Driver = SQLite3
Database = $(pwd)/pdns.sqlite3

[pdns-sqlite3-2]
Driver = SQLite3
Database = $(pwd)/pdns.sqlite32

[pdns-mssql]
Driver=FreeTDS
Trace=No
Server=pdns-odbc-regress-sql-1.database.windows.net
Port=1433
Database=pdns
TDS_Version=7.1
ClientCharset=UTF-8
__EOF__

	set +x
	. ~/.mssql-credentials
	set -x
	export GODBC_SQLITE3_DSN=pdns-sqlite3-1
	./timestamp timeout 120s ./start-test-stop 5300 godbc_sqlite3-nodnssec || EXITCODE=1
	export GODBC_MSSQL_DSN=pdns-mssql
	export GODBC_MSSQL_USERNAME
	export GODBC_MSSQL_PASSWORD
	./timestamp timeout 3600s ./start-test-stop 5300 godbc_mssql-nodnssec || EXITCODE=1
	./timestamp timeout 3600s ./start-test-stop 5300 godbc_mssql || EXITCODE=1
	./timestamp timeout 3600s ./start-test-stop 5300 godbc_mssql-nsec3 || EXITCODE=1
	./timestamp timeout 3600s ./start-test-stop 5300 godbc_mssql-nsec3-optout || EXITCODE=1
	./timestamp timeout 3600s ./start-test-stop 5300 godbc_mssql-nsec3-narrow || EXITCODE=1
fi

exit $EXITCODE
